//
//  SocketLogger.swift
//  QHSocketIODemo
//
//  Created by chen on 2017/5/17.
//  Copyright © 2017年 chen. All rights reserved.
//

import Foundation

public protocol SocketLogger: class {
    /// Whether to log or not
    var log: Bool { get set }
    
    /// Normal log messages
    func log(_ message: String, type: String, args: Any...)
    
    /// Error Messages
    func error(_ message: String, type: String, args: Any...)
}

public extension SocketLogger {
    func log(_ message: String, type: String, args: Any...) {
        abstractLog("LOG", message: message, type: type, args: args)
    }
    
    func error(_ message: String, type: String, args: Any...) {
        abstractLog("ERROR", message: message, type: type, args: args)
    }
    
    private func abstractLog(_ logType: String, message: String, type: String, args: [Any]) {
        guard log else { return }
        
        let newArgs = args.map({arg -> CVarArg in String(describing: arg)})
        let messageFormat = String(format: message, arguments: newArgs)
        
        NSLog("\(logType) \(type): %@", messageFormat)
    }
}

class DefaultSocketLogger: SocketLogger {
    static var Logger: SocketLogger = DefaultSocketLogger()
    
    var log = false
}
